package uit.com.project.iCampus.xmlparser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import uit.com.project.iCampus.model.User;

public class UserXMLHandler extends DefaultHandler {
	Boolean currentElement = false;
	String currentValue = null;
	private List<User> users = new ArrayList<User>();
	private User user = null;
	//private NhomNguoiDung nhomNguoiDung = null;

	/**
	 * @return the users
	 */
	public List<User> getUsers() {
		return users;
	}

	/**
	 * @param users the users to set
	 */
	public void setUsers(List<User> users) {
		this.users = users;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}


	/**
	 * Called when tag starts ( ex:- <name>AndroidPeople</name> -- <name> )
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currentElement = true;
		String element = (qName.equals("") ? localName : qName);
		if (element.equals(XMLParserAndroidUtil.ELEMENT)){
			if (attributes.getValue(0).equals("user")){
				user = new User();
				users.add(user);
			}
		} else if (element.equals("user")) {
			user = new User();
			users.add(user);
		} 
	}

	
	/**
	 * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		currentElement = false;
		String element = (qName.equals("") ? localName : qName);
		/** set value */
		if (element.equalsIgnoreCase("username"))
			user.setUsername(currentValue);
		else if (element.equalsIgnoreCase("password"))
			user.setPassword(currentValue);
		else if (element.equalsIgnoreCase("fullName"))
			user.setFullName(currentValue);
	}

	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String chars = (new String(ch).substring(start, start + length));
		if (currentElement) {
			currentValue = chars;
		}
	}
}
